<?php

class UserModel extends Model{

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getInfo($where){
        return $this->table("__USER__ as A ")
                    ->join('left join __AUTH_GROUP_ACCESS__ as B ON A.uid = B.uid ')
                    ->join(' left join __AUTH_GROUP__ as C ON B.group_id = C.id')
                    ->field('A.*,C.status as group_status,C.name as group_name')
                    ->where($where)
                    ->find();
    }

    /**
     * 登录用户
     * @param int $userId ID
     * @return bool 登录状态
     */
    public function setLogin($userId)
    {
        // 更新登录信息
        $data = array(
            'login_time' => NOW_TIME,
            'login_ip' => get_client_ip(),
        );
        $this->where(array('uid'=>$userId))->save($data);
        //设置cookie
        $auth = array(
            'uid' => $userId,
        );
        Session::set('admin_user', $auth);
        Session::set('admin_user_sign', data_auth_sign($auth));
        return true;
    }

    /**
     * 检测用户是否登录
     * @return int 用户ID
     */
    public function checkLogin(){
        $user = Session::get('admin_user');
        if (empty($user)) {
            return 0;
        } else {
            return Session::get('admin_user_sign') == data_auth_sign($user) ? $user['uid'] : 0;
        }
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){
        Session::del('admin_user');
        Session::del('admin_user_sign');
    }
}